package com.pactera.madp.cp.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.entity.clientLossManagement.RemindDeleteEntity;
import com.pactera.madp.cp.api.vo.remind.RemindDeleteVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.clientLossManagement.RemindDeleteMapper;
import com.pactera.madp.cp.service.RemindDeleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @date: 2021/7/26 16:34
 * @description:
 */
@Service
public class RemindDeleteServiceImpl extends ServiceImpl<RemindDeleteMapper, RemindDeleteEntity> implements RemindDeleteService {

    @Autowired
    RemindDeleteService remindDeleteService;

    /**
     *   开启或关闭
     */
    @Override
    public R selectSwitch(Integer status) {
        boolean flag = false;
        List<RemindDeleteEntity> list = this.list(Wrappers.<RemindDeleteEntity>lambdaQuery().eq(RemindDeleteEntity::getCorpId,CorpContextHolder.getCorpId()));
        if(list.isEmpty()) {
            RemindDeleteEntity remindDeleteEntity = new RemindDeleteEntity();
            remindDeleteEntity.setStatus(status);
            remindDeleteEntity.setCorpId(CorpContextHolder.getCorpId());
            flag = this.save(remindDeleteEntity);
        }else {
            RemindDeleteEntity remindDeleteEntity = list.get(0);
            remindDeleteEntity.setStatus(status);
            remindDeleteEntity.setCorpId(CorpContextHolder.getCorpId());
            flag = this.updateById(remindDeleteEntity);
        }

        if(flag) {
            return R.ok();
        }
        return R.failed();
    }

    @Override
    public R queryStatus() {
        List<RemindDeleteEntity> list = this.list(Wrappers.<RemindDeleteEntity>lambdaQuery().eq(RemindDeleteEntity::getCorpId,CorpContextHolder.getCorpId()));
        RemindDeleteVO vo = new RemindDeleteVO();
        for(RemindDeleteEntity entity:list){
            vo.setStatus(entity.getStatus());
        }
        return R.ok(vo);
    }


}
